﻿using DotNet.Utilities;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;
using Maticsoft.BLL;
namespace _58
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public new enum Site
        {
            /// <summary>
            /// 58同城
            /// </summary>
            wuba,
            /// <summary>
            /// 赶集网
            /// </summary>
            ganji
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            object[] item = new object[] { "58同城", "赶集网" };
            cmb_site.Items.AddRange(item);
            cmb_site.SelectedText = "赶集网";
            FirstUrls = new List<string>();
            ConpanyUrls = new List<string>();
            Conpanys_Model = new List<Conpany>();
            toolStripStatusLabel1.Text = "状态：";
            toolStripStatusLabel2.Text = "输入完成后，执行第一步获取url";
            btn_get_enterprise.Enabled = false;
            btn_get_info.Enabled = false;

        }
        private void GetFirstUrl(object param)
        {
            object obj = new object();
            string href = param.ToString();
            lock (obj)
            {
                List<string> urls = new List<string>();
                HttpHelper http = new HttpHelper();
                HttpItem item = new HttpItem()
                {
                    URL = href,//URL     必需项
                    Encoding = null,//编码格式（utf-8,gb2312,gbk）     可选项 默认类会自动识别
                                    //Encoding = Encoding.Default,
                    Method = "get",//URL     可选项 默认为Get
                    Timeout = 100000,//连接超时时间     可选项默认为100000
                    ReadWriteTimeout = 30000,//写入Post数据超时时间     可选项默认为30000
                    IsToLower = false,//得到的HTML代码是否转成小写     可选项默认转小写
                    Cookie = "",//字符串Cookie     可选项
                    UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",//用户的浏览器类型，版本，操作系统     可选项有默认值
                    Accept = "text/html, application/xhtml+xml, */*",//    可选项有默认值
                    ContentType = "text/html",//返回类型    可选项有默认值
                    Referer = "http://www.apic.in",//来源URL     可选项
                    Allowautoredirect = true,//是否根据３０１跳转     可选项
                    Connectionlimit = 1024,//最大连接数     可选项 默认为1024
                    CookieCollection = new System.Net.CookieCollection(),//可以直接传一个Cookie集合进来
                };


                //得到HTML代码
                HttpResult result = http.GetHtml(item);
                //取出返回的Cookie
                string cookie = result.Cookie;
                //返回的Html内容
                string html = result.Html;




                if (result.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    //表示访问成功，具体的大家就参考HttpStatusCode类
                    string pattern = Parttern.List_Pattern;
                    MatchCollection mc = Regex.Matches(html, pattern);
                    foreach (Match m in mc)
                    {
                        if (m.Success)
                        {

                            //去重企业，有的企业一个页面上可能有多个招聘信息
                            if (!FirstUrls.Contains(m.Groups["url"].Value))
                            {
                                FirstUrls.Add(m.Groups["url"].Value);
                            }

                        }
                    }

                }
                //FirstUrls.AddRange(urls);
            }
            //for (int i = 0; i < FirstUrls.Count; i++)
            //{
            //    System.Diagnostics.Debug.WriteLine(FirstUrls[i]);
            //}
        }
        private void GetSecondUrl(object param)
        {
            object obj = new object();
            string href = param.ToString();
            lock (obj)
            {
                List<string> urls = new List<string>();
                HttpHelper http = new HttpHelper();
                HttpItem item = new HttpItem()
                {
                    URL = href,//URL     必需项
                    Encoding = null,//编码格式（utf-8,gb2312,gbk）     可选项 默认类会自动识别
                                    //Encoding = Encoding.Default,
                    Method = "get",//URL     可选项 默认为Get
                    Timeout = 100000,//连接超时时间     可选项默认为100000
                    ReadWriteTimeout = 30000,//写入Post数据超时时间     可选项默认为30000
                    IsToLower = false,//得到的HTML代码是否转成小写     可选项默认转小写
                    Cookie = "",//字符串Cookie     可选项
                    UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",//用户的浏览器类型，版本，操作系统     可选项有默认值
                    Accept = "text/html, application/xhtml+xml, */*",//    可选项有默认值
                    ContentType = "text/html",//返回类型    可选项有默认值
                    Referer = "http://www.apic.in",//来源URL     可选项
                    Allowautoredirect = true,//是否根据３０１跳转     可选项
                    Connectionlimit = 1024,//最大连接数     可选项 默认为1024
                    CookieCollection = new System.Net.CookieCollection(),//可以直接传一个Cookie集合进来
                };


                //得到HTML代码
                HttpResult result = http.GetHtml(item);
                //取出返回的Cookie
                string cookie = result.Cookie;
                //返回的Html内容
                string html = result.Html;




                if (result.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    //表示访问成功，具体的大家就参考HttpStatusCode类
                    string pattern = Parttern.Detial_Parttern;
                    MatchCollection mc = Regex.Matches(html, pattern);
                    foreach (Match m in mc)
                    {
                        if (m.Success)
                        {

                            //去重企业，有的企业一个页面上可能有多个招聘信息
                            if (!ConpanyUrls.Contains(m.Groups["id"].Value))
                            {
                                ConpanyUrls.Add("http://" + m.Groups["id"].Value + ".5858.com");
                            }

                        }
                    }

                }
            }

        }
        private void GetConpanyInfo(object param)
        {
            object obj = new object();
            string href = param.ToString();
            lock (obj)
            {
                HttpHelper http = new HttpHelper();
                HttpItem item = new HttpItem()
                {
                    URL = href,//URL     必需项
                    Encoding = null,//编码格式（utf-8,gb2312,gbk）     可选项 默认类会自动识别
                                    //Encoding = Encoding.Default,
                    Method = "get",//URL     可选项 默认为Get
                    Timeout = 100000,//连接超时时间     可选项默认为100000
                    ReadWriteTimeout = 30000,//写入Post数据超时时间     可选项默认为30000
                    IsToLower = false,//得到的HTML代码是否转成小写     可选项默认转小写
                    Cookie = "",//字符串Cookie     可选项
                    UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",//用户的浏览器类型，版本，操作系统     可选项有默认值
                    Accept = "text/html, application/xhtml+xml, */*",//    可选项有默认值
                    ContentType = "text/html",//返回类型    可选项有默认值
                    Referer = "http://www.apic.in",//来源URL     可选项
                    Allowautoredirect = true,//是否根据３０１跳转     可选项
                    Connectionlimit = 1024,//最大连接数     可选项 默认为1024
                    CookieCollection = new System.Net.CookieCollection(),//可以直接传一个Cookie集合进来
                };


                //得到HTML代码
                HttpResult result = http.GetHtml(item);
                //取出返回的Cookie
                string cookie = result.Cookie;
                //返回的Html内容
                string html = result.Html;




                if (result.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    //表示访问成功，具体的大家就参考HttpStatusCode类
                    string pattern = Parttern.Conpany_Parttern;
                    MatchCollection mc = Regex.Matches(html, pattern);
                    foreach (Match m in mc)
                    {
                        if (m.Success)
                        {
                            //if (!ConpanyUrls.Contains(m.Groups["id"].Value))
                            //{
                            //    ConpanyUrls.Add("http://" + m.Groups["id"].Value + ".5858.com");
                            //}
                            Conpanys_Model.Add(new Conpany()
                            {
                                Address = m.Groups["Address"].Value,
                                Contacts = m.Groups["Contacts"].Value,
                                Name = m.Groups["ConpanyName"].Value,
                                Phone = m.Groups["phone"].Value,
                            }
                            );
                        }
                    }

                }
            }

        }
        public static int FuckSite { get; set; }
        public List<string> FirstUrls { get; set; }
        public List<string> ConpanyUrls { get; set; }
        public List<Conpany> Conpanys_Model { get; set; }
        public int StartPage { get { return int.Parse(txt_start_page.Text); } }
        public int EndPage { get { return int.Parse(txt_end_page.Text); } }
        private void btn_get_conpay_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();
            string url = txt_list_url.Text;
            FirstUrls.Clear();
            string isParttern;
            if (cmb_site.Text == "58同城")
            {
                FuckSite = 0;
                isParttern = "pn\\d+/\\?PGTID=";
            }
            else
            {
                FuckSite = 1;
                isParttern = "o\\d+";
            }
            ConpanyUrls.Clear();
            Conpanys_Model.Clear();
            List<Thread> arr = new List<Thread>();

            if (!Regex.IsMatch(url, isParttern))
            {
                //文本框中没有页码，这里给加上才能继续
                if (FuckSite == Convert.ToInt32(Site.wuba))
                {
                    url = url.Replace("/?PGTID", "/pn1/?PGTID");
                }
                else
                {
                    url += "o1";
                }
            }
            string s, pageFlag;
            if (FuckSite == Convert.ToInt32(Site.wuba))
            {

                pageFlag = "/pn";
            }
            else
            {
                pageFlag = "/o";
            }
            for (int i = StartPage; i <= EndPage; i++)
            {
                if (FuckSite == Convert.ToInt32(Site.wuba))
                {
                    s = url.Substring(url.LastIndexOf("/?PGTID") - 4, 4);
                }
                else
                {
                    s = url.Substring(url.LastIndexOf("/o"), 3);
                }
                url = url.Replace(s, pageFlag + i);
                Thread thread = new Thread(GetFirstUrl);
                thread.Start(url);
                arr.Add(thread);

            }
            foreach (Thread t in arr)
            {
                while (t.IsAlive)
                {
                    Thread.Sleep(10);
                }
            }
            toolStripStatusLabel2.Text = "第一步获取url集合完成，请执行第二步";
            btn_get_conpay.Enabled = false;
            if (FuckSite == Convert.ToInt32(Site.wuba))
            {
                btn_get_enterprise.Enabled = true;
            }
            else
            {
                btn_get_enterprise.Enabled = false;
                btn_get_info.Enabled = true;
            }

        }

        private void btn_get_enterprise_Click(object sender, EventArgs e)
        {
            #region output
            int j = 0;
            foreach (var item in FirstUrls)
            {
                j++;
                System.Diagnostics.Debug.WriteLine(j + "=" + item);
            }
            #endregion

            List<Thread> arr = new List<Thread>();
            for (int i = 0; i < FirstUrls.Count; i++)
            {
                Thread thread = new Thread(GetSecondUrl);
                thread.Start(FirstUrls[i]);
                arr.Add(thread);
            }
            foreach (Thread t in arr)
            {
                while (t.IsAlive)
                {
                    Thread.Sleep(10);
                }
            }
            toolStripStatusLabel2.Text = "第二步获取公司网址集合完成，请执行最后一步获取公司信息";
            btn_get_conpay.Enabled = false;
            btn_get_enterprise.Enabled = false;
            btn_get_info.Enabled = true;
        }

        private void btn_get_info_Click(object sender, EventArgs e)
        {
            #region output
            int j = 0;
            foreach (var item in ConpanyUrls)
            {
                j++;
                System.Diagnostics.Debug.WriteLine(j + "=" + item);
            }
            #endregion

            if (FuckSite == Convert.ToInt32(Site.ganji))
            {
                ConpanyUrls = FirstUrls;

                for (int i = 0; i < ConpanyUrls.Count; i++)
                {
                    ConpanyUrls[i] += "contactus/#tabl";
                }
            }

            List<Thread> arr = new List<Thread>();
            for (int i = 0; i < ConpanyUrls.Count; i++)
            {
                Thread thread = new Thread(GetConpanyInfo);
                thread.Start(ConpanyUrls[i]);
                arr.Add(thread);
            }
            foreach (Thread t in arr)
            {
                while (t.IsAlive)
                {
                    Thread.Sleep(10);
                }
            }
            toolStripStatusLabel2.Text = "采集完毕！";
            btn_get_conpay.Enabled = true;
            btn_get_enterprise.Enabled = false;
            btn_get_info.Enabled = false;
            int count = 0;
            foreach (Conpany conpany in Conpanys_Model)
            {
                count++;
                ListViewItem item = new ListViewItem(count.ToString(), 0);
                item.SubItems.Add(conpany.Name);
                item.SubItems.Add(conpany.Contacts);
                item.SubItems.Add(conpany.Email);
                item.SubItems.Add(conpany.Phone);
                item.SubItems.Add(conpany.Address);
                listView1.Items.Add(item);

            }

        }

        private void btn_export_Click(object sender, EventArgs e)
        {
            conpany bll = new conpany();
            int successCount = 0, faildCount = 0;
            for (int i = 0; i < listView1.Items.Count; i++)
            {
                if (!bll.Exists(listView1.Items[i].SubItems[1].Text))
                {
                    Maticsoft.Model.conpany c = new Maticsoft.Model.conpany()
                    {
                        Address = listView1.Items[i].SubItems[5].Text,
                        ConpanyName = listView1.Items[i].SubItems[1].Text,
                        Contacts = listView1.Items[i].SubItems[2].Text,
                        Phone = listView1.Items[i].SubItems[4].Text,
                        Source = FuckSite == 0 ? "58同城" : "赶集网"
                    };

                    if (bll.Add(c) > 0)
                    {
                        successCount++;
                    }

                }
                else
                {
                    faildCount++;
                }
            }

            MessageBox.Show("导出成功" + successCount + "个\r\n失败" + faildCount + "个");

        }

        private void cmb_site_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmb_site.Text == "58同城")
            {

                txt_list_url.Text = "http://luoyang.58.com/zplvyoujiudian/pn5/?PGTID=0d30365b-0022-c17b-aee9-5d90e0dff04b&ClickID=1";
            }
            else
            {
                txt_list_url.Text = "http://luoyang.ganji.com/zhuangxiu/o2/";
                btn_get_enterprise.Enabled = false;
            }
        }
    }
}
